<html>

<head>

${inc:/action/pub/common}
<title>费用发放</title>
</head>

<body>


<!-- <br> -->
<center>

<div id="form">
<form name="formEditor" onsubmit="return update()">
    
<TABLE class="table_comm" cellSpacing=0 cellPadding=0 width="100%" border=0>
<!-- 页面内容标签：如“新增记录”、“修改记录” -->
<tr>
<td>
<div id=tabs>
    <ul>
        <li onclick="changeLiStyle(this);" id="tabsl">
            <a  
                onmouseover="javascript:window.status='';return true;" 
                onmouseout="javascript:window.status='';return true;" 
                href='#' 
                onclick='JavaScript:return false;'>
                <span id="formTitleEditor">费用发放信息</span>
            </a>
        </li>
    </ul>
    <div id='hline'></div>
</div>
</tr>
</td>
</table>

<FIELDSET><LEGEND>费用发放信息</LEGEND>
<TABLE class="table_comm" cellSpacing=0 cellPadding=0 width="100%" border=0>
    <!-- <tr>
        <td class="th20 required">项目名称</td>
        <td class="td20left">
            
        </td>
    </tr> -->


    <tr>
        <td class="th20 required">户主</td>
        <td class="td20left">
            ${fld:master_name@js}
        </td>
    </tr>
    <tr>
        <td class="th20">所在村名名称</td>
        <td class="td20left">
            ${fld:cun_name@js}
        </td>
    </tr>
    <tr>
        <td class="th20">所在屯(队)名称</td>
        <td class="td20left">
            ${fld:tun_name@js}
        </td>
    </tr>
    <tr>
        <td class="td20left" colspan="2"></td>
    </tr>
    <tr>
        <td class="th20">历史费用发放记录</td>
        <td class="td20left">
            <!-- 户主名称<input type="text" name="master_name_ok" value = "${fld:master_name@js}">
            <input type="button" name="searchMaster" class="cmd" value="查找记录" onclick="javascript:findMaster();">
            <br> -->
            <select name="fee_ok_tuid" id="fee_ok_tuid" onchange="javascript:confirmFee();">
                <option value="">--费用发放历史--</option>
                <rows>
                    <option value="${fld:tuid}">${fld:master_name} # ${fld:touqin_fee@#######0.00} # ${fld:begin_date} # ${fld:end_date} # [共${fld:month_count}月](${fld:create_date@yyyy年MM月dd日}创建)</option>
                </rows>
            </select>
            <!-- <input type="button" name="searchMaster" class="cmd" value="选择" onclick="javascript:confirmFee();"> -->
        </td>
    </tr>
    <tr>
        <td class="th20">实际情况</td>
        <td class="td20left">
            投亲开始时间:<input id="begin_date" name="begin_date" type="text" size="10" maxlength="25" value="">
            <img class="tool" src="${def:context}/images/calendar.gif" title="打开日历.." onclick="calendarOpen('begin_date')" onblur="evalEnddate()"> YYYY-MM-DD<br>
            <br>
            投亲结束时间:<input id="end_date" name="end_date" type="text" size="10" maxlength="25" value="">
            <img class="tool" src="${def:context}/images/calendar.gif" title="打开日历.." onclick="calendarOpen('end_date')"> YYYY-MM-DD<br>
            已经发放费用<input id="touqin_fee_ok" name="touqin_fee_ok" type="text" size="10" maxlength="25" value="0" >

            <br><br>实际每月投亲费用:
                    <select name="month_price_real" id="month_price_real">
                        <option value="600">600</option>
                        <option value="700">700</option>
                        <option value="800">800</option>
                    </select>
            单位:(元/月))<br>
            实际结束时间:<input id="end_date_real" name="end_date_real" type="text" size="10" maxlength="25" value="">
                        <img class="tool" src="${def:context}/images/calendar.gif" title="打开日历.." onclick="calendarOpen('end_date_real');;"> YYYY-MM-DD 
            <input type="button" name="getFeeReal" class="cmd" value="实际费用" onclick="javascript:getMonthdiff();">
            实际月份数:<input id="month_count_real" name="month_count_real" type="text" size="10" maxlength="64" value="" readonly>
            实际费用<input id="touqin_fee_real" name="touqin_fee_real" type="text" size="10" maxlength="25" value="" onblur="getRealValue();">
        </td>
    </tr>
    <tr>
        <td class="td20left" colspan="2"></td>
    </tr>
    <tr>
        <td class="th20 required">费用发放状态</td>
        <td class="td20left">
            <input type="radio" name="fee_status" value="0" checked>未发放<br>
            <input type="radio" name="fee_status" value="1">已发放
        </td>
    </tr>
    <tr>
        <td class="th20">人口数量</td>
        <td class="td20left">
            <input type="text" name="population" value="" onkeydown="changeMyFocusByKey('','house_number')">
        </td>
    </tr>
    <tr>
        <td class="th20">原房间数</td>
        <td class="td20left">
            <input type="text" name="house_number" value="" onkeydown="changeMyFocusByKey('population','banjia_fee')">
        </td>
    </tr>
    <tr>
        <td class="th20">搬家费</td>
        <td class="td20left">
            <input type="text" name="banjia_fee" value="" onblur="evalTotal();" onkeydown="changeMyFocusByKey('house_number','touqin_fee')">
        </td>
    </tr>
    <tr>
        <td class="th20">投亲费</td>
        <td class="td20left">
            <input type="text" name="touqin_fee" value="" onblur="evalTotal();" onkeydown="changeMyFocusByKey('banjia_fee','suqian_fee')">
        </td>
    </tr>
    <tr>
        <td class="th20">速迁费</td>
        <td class="td20left">
            <input type="text" name="suqian_fee" value="" onblur="evalTotal();" onkeydown="changeMyFocusByKey('touqin_fee','other_fee')">
        </td>
    </tr>
    <tr>
        <td class="th20">其他费用</td>
        <td class="td20left">
            <input type="text" name="other_fee" value="" onblur="evalTotal();" onkeydown="changeMyFocusByKey('suqian_fee','receive_user')">
        </td>
    </tr>
    <tr>
        <td class="th20">合计</td>
        <td class="td20left">
            <input type="text" name="total_fee" value="" readonly>
        </td>
    </tr>
    <tr>
        <td class="th20">领取人</td>
        <td class="td20left">
            <input type="text" name="receive_user" value="" onkeydown="changeMyFocusByKey('other_fee','remark')">
        </td>
    </tr>
    <tr>
        <td class="th20">备注</td>
        <td class="td20left">
            <input type="text" name="remark" value="" onkeydown="changeMyFocusByKey('receive_user','')">
        </td>
    </tr>
</TABLE>
</FIELDSET>

<FIELDSET><LEGEND>操作信息</LEGEND>
<TABLE class=table_comm cellSpacing=0 cellPadding=0 width="100%" border=0>
        <col width="80%">
        <col width="20%">
        <!--buttons-->
        <tr>
            <td align="center" colspan=1>
                <input type="submit" class="cmd" value="保存">
                <!-- <input type="button" class="cmd" value="返回" onclick="history.back()" > -->
            </td>
            <td class="th20 required" colspan=3>
                特别标注为必填信息.
            </td>
        </tr>
    
</table>
</FIELDSET>

    <input type="hidden" name="tuid" value=""><!-- t_fee.tuid -->
    <input type="hidden" name="master_tuid" value="${fld:s_tuid}"><!-- t_master.tuid -->
    </form>
</div>

<!--panel de respuesta AJAX #2 insert/update-->
<div id="response" style="display:none;width:100%">
</div>

<!--indicador de progreso-->
<!-- <br> -->
<div id="status" class="alert" style="text-align:left; display:none;color:gray;width:150px;font-weight:bold;font-family:Arial">
    <img src="${def:context}/images/progress.gif"> 请稍候！
</div>

<!--insert/update response-->
<div id="divEditorResponse" style="display:none;width:100%">
</div>


<!--progress indicator-->
<!-- <br> -->
<div id="divStatus" class="alert" style="text-align:left; display:none;color:gray;width:150px;font-weight:bold;font-family:Arial">
	<img src="${def:context}/images/progress.gif"> 请稍候...
</div>

</center>
<!--codigo de soporte del template-->
<script type="text/javascript">
    //返回
    function goBack() 
    {
        _goBackFn()         
    }
    
    //加载数据
    function edit(id)
    {
            //llamada Ajax...
            ajaxCall(httpMethod="GET", 
                            uri="${def:actionroot}/edit?id=" + id, 
                            divResponse="form", 
                            divProgress="status", 
                            formName=null, 
                            afterResponseFn=null, 
                            onErrorFn=null);    
    }
	function parseFloatExtend(val){
		if(val == ""){
			return 0;
		}
		return Math.round(parseFloat(val)*100)/100;
	}
    //合计
    function evalTotal(){
        var banjia_fee = document.formEditor.banjia_fee.value;
        var touqin_fee = document.formEditor.touqin_fee.value;
        var suqian_fee = document.formEditor.suqian_fee.value;
        var other_fee = document.formEditor.other_fee.value;

        var total_fee = document.formEditor.total_fee;
        total_fee.value = parseFloatExtend(banjia_fee)+parseFloatExtend(touqin_fee)+parseFloatExtend(suqian_fee)+parseFloatExtend(other_fee);

    }
    //更新数据
    function update()
    {       
            //definir funcion goBack()
            _goBackFn = showEdit;
            
            //esconder formulario principal
            //document.getElementById("form").style.display="none";

        var tuid = document.forms["formEditor"].elements["tuid"].value;
        var newFlag = (tuid==""?true:(tuid=="0"?true:false));   /*界面中须要有tuid标识主键,为空是新增记录*/
        if (newFlag){
            //llamada Ajax...
            return ajaxCall(httpMethod="POST", 
                            uri="${def:actionroot}/insert", 
                            divResponse="response", 
                            divProgress="status", 
                            formName="formEditor", 
                            afterResponseFn=null, 
                            onErrorFn=showEdit);
        }
        else{
            //llamada Ajax...
            return ajaxCall(httpMethod="POST", 
                            uri="${def:actionroot}/update", 
                            divResponse="response", 
                            divProgress="status", 
                            formName="formEditor", 
                            afterResponseFn=null, 
                            onErrorFn=showEdit);
        }
            
    }
    
    //muestra el formulario principal
    function showForm()
    {
        document.getElementById("form").style.display="";
//        document.getElementById("grabar").style.display="none";
//        document.getElementById("cancelar").style.display="none";
//        document.getElementById("formTitle").innerHTML = "Registro";
//        clearForm("formEditor");
//        document.formEditor.lname.focus();
        
    }
    
    //muestra el formulario principal para editar
    function showEdit()
    {
            //alert("showEdit");
            document.getElementById("form").style.display="";
            document.getElementById("response").style.display="none";
    //        document.getElementById("grabar").style.display="";
    //        document.getElementById("formTitle").innerHTML = "Editar registro";
    //        document.formEditor.lname.focus();
        
    }

    //加载费用发放信息
    edit("${fld:s_tuid}");

	function changeMyFocusByKey(up,down){
	var evt = getEvent();
	var keycode = evt.keyCode?evt.keyCode:evt.which;
	switch (keycode)
	{
		case 38:
			if(up == "") return;
			document.forms["formEditor"].elements[up].focus();
			document.forms["formEditor"].elements[up].select();
			break;
		case 40:
			if(down == "") return;
			document.forms["formEditor"].elements[down].focus();
			document.forms["formEditor"].elements[down].select();
			break;
	}
  }

  function getEvent() //同时兼容ie和ff的写法 
    {  
        if(document.all)  return window.event;    
        func=getEvent.caller;        
        while(func!=null){  
            var arg0=func.arguments[0]; 
            if(arg0) 
            { 
              if((arg0.constructor==Event || arg0.constructor ==MouseEvent) || (typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation)) 
              {  
              return arg0; 
              } 
            } 
            func=func.caller; 
        } 
        return null; 
    } 

    //匹配户主 2010-8-2 anthony
function findMaster()
{
    var master_name = document.forms["formEditor"].elements["master_name_ok"].value;
    master_name = encodeURIComponent(master_name);
	return ajaxCall(httpMethod="GET", 
                        uri="${def:actionroot}/findmaster?master_name_ok="+master_name, 
                        divResponse="divEditorResponse", 
                        divProgress="divStatus", 
                        formName=null, 
                        afterResponseFn=null, 
                        onErrorFn=null);
}
//确认费用
function confirmFee(){
    var begin_date = document.forms["formEditor"].elements["begin_date"];
    var end_date = document.forms["formEditor"].elements["end_date"];
    var touqin_fee_ok = document.forms["formEditor"].elements["touqin_fee_ok"];
    var fee_ok_tuid = document.forms["formEditor"].elements["fee_ok_tuid"];

    var selectedValue = fee_ok_tuid.options[fee_ok_tuid.selectedIndex].text;
    if (selectedValue.indexOf(" # ") >0)
    {
        var arrayVal = selectedValue.split(" # ");
        touqin_fee_ok.value = arrayVal[1];
        begin_date.value = arrayVal[2];
        end_date.value = arrayVal[3];
    }
}
//计算费用
function getRealValue(){
    var touqin_fee_ok = document.forms["formEditor"].elements["touqin_fee_ok"];
    var touqin_fee_real = document.forms["formEditor"].elements["touqin_fee_real"];
    var touqin_fee = document.forms["formEditor"].elements["touqin_fee"];

    var innerValue = parseFloat(touqin_fee_real.value) - parseFloat(touqin_fee_ok.value);
    touqin_fee.value = innerValue;

    evalTotal();
}
function getMonthdiff(){
    //确定真实月份
//    var begin_date = document.forms["formEditor"].elements["begin_date"].value;
//    var end_date_real = document.forms["formEditor"].elements["end_date_real"].value;
//
//    //投亲真实月份数
//    var touqin_months = datespace(begin_date,end_date_real);
//    document.forms["formEditor"].elements["touqin_fee_real"].value = parseFloat(touqin_months) * 700;

    calMonthCount();
    //计算实际投亲费用
    getRealValue();
    
}
//计算日期间隔
    function   datespace(firsttime,endtime){   
    var firsttime= firsttime.replace(/-/g, "\/");//转换   
    var endtime=   endtime.replace(/-/g, "\/");   
    var   etime=   new   Date(endtime)   
    var   ftime=   new   Date(firsttime)   


    var   ilemonth   =   etime.getMonth()   -   ftime.getMonth()   ;
    var   ileyear=etime.getYear()   -   ftime.getYear()   ;
    var   ile=ileyear*12+ilemonth   ;
    //document.all.space.value=ile   ;
    //1-10算正月；11-20算半月；21以后不算
    var diff = 0;
    var dateValue = ftime.getDate();
    if (dateValue<=10)
    {
        diff = 1;
    }else if (dateValue>10 && dateValue<=20)
    {
        diff = 0.5;
    }else if (dateValue>20)
    {
        diff = 0;
    }
    return (ile+diff);
  }

    //根据开始时间、结束时间计算月份数
    function calMonthCount(){
        //校验
        //校验：价格
        //if(!CheckNum("month_price_real","formEditor")){
        //    return;
        //}

        //校验：时间
        var begin_date = document.getElementById("begin_date").value;
        var end_date_real = document.getElementById("end_date_real").value;

        if(!CheckLength("begin_date")){
            return;
        }
        if(!CheckLength("end_date_real")){
            return;
        }


        beginDate = begin_date.replace(/-/g, "\/");//转换
        endDate   = end_date_real.replace(/-/g, "\/");
        var   etime=   new   Date(endDate);
        var   btime=   new   Date(beginDate);
        var   ilemonth   =   etime.getMonth() - btime.getMonth();
        var   ileyear=etime.getYear() - btime.getYear();
        var   ile=ileyear*12+ilemonth+1;
        if(ile<=0){
            alert("投亲开始时间、结束时间不合法!");
        }else{
            beginDate_day = btime.getDate();
            var i=0;
            if(beginDate_day>0 && beginDate_day<=10){
                i=1;
            }else if(beginDate_day>10 && beginDate_day<=20){
                i=0.5;
            }else if(beginDate_day>20){
                i=0;
            }

            //根据结束日期决定月份(1-10:0 11-20:0.5)
            endDate_day = etime.getDate();
            var j=0;
            if(endDate_day>0 && endDate_day<=10){
                j=0;
            }else if(endDate_day>10 && endDate_day<=20){
                j=0.5;
            }else if(endDate_day>20){
                j=1;
            }

            //月份数
            ile = ile-1+i;
            ile = ile-1+j;
            document.getElementById("month_count_real").value = ile;
            
            //已经发放费用如果为空，则置为0
            if(document.formEditor.touqin_fee_ok.value==""){
                document.formEditor.touqin_fee_ok.value="0";
            }

            //计算投亲费用
            var month_price_real = document.forms["formEditor"].elements["month_price_real"];
            var selectedValue = month_price_real.options[month_price_real.selectedIndex].value;

            document.formEditor.touqin_fee_real.value = parseFloatExtend(parseFloat(selectedValue) * parseFloat(ile));
        }
    }
</script>
</body>
</html>
